#include <map>
#include <string>
#include <vector>
#include <cassert>
#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

map<string, string> stemming_back;
vector<string> before_stemming;
vector<string> after_stemming;
map<string, vector<pair<int, double> > > index;

int main(int argc, const char *argv[])
{
    // 1 load index
    ifstream fin_index("index");
    string word;
    string line;
    int id;
    double val;

    while (fin_index >> word) {
        getline(fin_index, line);
        getline(fin_index, line);
        stringstream ss(line);
        vector<pair<int, double> >& cur = index[word];
        while (ss >> id >> val) {
            cur.push_back(make_pair(id, val));
        }
    }
    fin_index.close();
/*
    // 2 stemming back
    ifstream fin_image("image_theme_list_stop_words_removed");
    ifstream fin_image_stemming("image_theme_list_stop_words_removed_stemming");

    while (fin_image >> word) {
        before_stemming.push_back(word);   
    }
    while (fin_image_stemming >> word) {
        after_stemming.push_back(word);
    }

    fin_image.close();
    fin_image_stemming.close();

    ifstream fin_music("music_theme_list_stop_words_removed");
    ifstream fin_music_stemming("music_theme_list_stop_words_removed_stemming");
        
    while (fin_music >> word) {
        before_stemming.push_back(word);   
    }
    while (fin_music_stemming >> word) {
        after_stemming.push_back(word);
    }

    fin_music.close();
    fin_music_stemming.close();

    assert(before_stemming.size() == after_stemming.size());

    for (int i = 0; i < before_stemming.size(); ++i) {
       stemming_back[after_stemming[i]] = before_stemming[i];
    }
*/

    // 3 combine index
    ifstream fin_image("image_theme_list_stop_words_removed_stemming");
    ofstream fout_image("image_theme_ESA_index");

    while (getline(fin_image, line)) {
        stringstream ss(line);
        while (ss >> word) {
            vector<pair<int, double> >& cur = index[word];   
            for (int i = 0; i < cur.size(); ++i) {
                fout_image << cur[i].first << ' ' << cur[i].second << ' ';
            }
        }
        fout_image << endl;
    }

    ifstream fin_music("music_theme_list_stop_words_removed_stemming");
    ofstream fout_music("music_theme_ESA_index");

    while (getline(fin_music, line)) {
        stringstream ss(line);
        while (ss >> word) {
            vector<pair<int, double> >& cur = index[word];   
            for (int i = 0; i < cur.size(); ++i) {
                fout_music << cur[i].first << ' ' << cur[i].second << ' ';
            }
        }
        fout_music << endl;
    }

    return 0;
}
